import { TriangleExclamation as ITriangleExclamation } from '@vexip-ui/icons'
import type { AlovaFrontMiddleware } from 'alova/client'
import { isArray } from 'lodash'
import { Confirm } from 'vexip-ui'

type ExpressionFunc = () => Promise<boolean>
type Middleware = AlovaFrontMiddleware<any>

export function middlewares(func: ExpressionFunc | ExpressionFunc[]): Middleware {
  return async (_, next) => {
    const $bool = await (isArray(func) ? func.some((x) => x()) : func())
    if ($bool) await next()
  }
}

export async function logoutDialog() {
  return await Confirm.open({
    title: '退出登录',
    content: '是否退出登录? 退出登录将清除登录状态, 但会保存你的配置文件在本地',
    confirmType: 'primary',
    icon: ITriangleExclamation,
    iconProps: { scale: 4 }
  })
}

export async function destroyDialog() {
  return await Confirm.open({
    title: '删除数据',
    content: '确认删除数据 ?',
    confirmType: 'error',
    icon: ITriangleExclamation,
    iconProps: { color: 'red', scale: 4 }
  })
}

export async function salaryPaidDialog() {
  return await Confirm.open({
    title: '标记为支付工资',
    content: '确认标记 ?',
    confirmType: 'warning',
    icon: ITriangleExclamation,
    iconProps: { scale: 4 }
  })
}

export async function paidDialog() {
  return await Confirm.open({
    title: '修改此订单为已支付状态',
    content: '此操作不可撤销, 执行后此数据将会标记为归档, 不得再修改, 请核对付款信息无误再操作',
    confirmType: 'warning',
    icon: ITriangleExclamation,
    iconProps: { scale: 4 }
  })
}
